﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ASMS.Excel2DbTool.Utils
{
    public class WaiyunProject
    {
        static string[] s_importantProjects = new string[]{
            "北车埃及空车体项目",
"特变电工安哥拉SK输变电建设项目",
"中信国华安哥拉项目",
"中钢阿尔及利亚钢厂项目",
"中交二航局巴基斯坦港口建设项目",
"北车齐齐哈尔配件运输项目",
"中铝委内瑞拉电解铝厂项目",
"中机泰国308台集装箱平车项目",
"成都院埃及GOE项目",
"中材海外安哥拉项目"};

        public static void Run(string filePath)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder coordJsonBuilder = new StringBuilder("{");
            StringBuilder projectJsonBuilder = new StringBuilder("[");

            List<string> deptList = new List<string>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string project = Convert.ToString(dataTable.Rows[index][1]).Trim();
                string source = Convert.ToString(dataTable.Rows[index][4]).Trim();
                string dest = Convert.ToString(dataTable.Rows[index][5]).Trim();
                string value = Convert.ToString(dataTable.Rows[index][6]).Trim();
                string pm = Convert.ToString(dataTable.Rows[index][7]).Trim();
                string sourceGEO = Convert.ToString(dataTable.Rows[index][8]).Trim();
                string destGEO = Convert.ToString(dataTable.Rows[index][9]).Trim();

                if (string.IsNullOrEmpty(destGEO) || string.IsNullOrEmpty(sourceGEO))
                    continue;

                string[] sourceCoordArr = sourceGEO.Split(new char[] { ',' });
                string[] destCoordArr = destGEO.Split(new char[] { ',' });
                if (sourceCoordArr.Length == 2 && destCoordArr.Length == 2)
                {
                    if (!deptList.Contains(source))
                    {
                        if (coordJsonBuilder.Length == 1)
                        {
                            coordJsonBuilder.AppendFormat("\"{0}\":[{1},{2}]", source, Convert.ToDecimal(sourceCoordArr[0]), Convert.ToDecimal(sourceCoordArr[1]));
                        }
                        else
                        {
                            coordJsonBuilder.AppendLine();
                            coordJsonBuilder.AppendFormat(",\"{0}\":[{1},{2}]", source, Convert.ToDecimal(sourceCoordArr[0]), Convert.ToDecimal(sourceCoordArr[1]));
                        }
                        deptList.Add(source);
                    }
                    if (!deptList.Contains(dest))
                    {
                        if (coordJsonBuilder.Length == 1)
                        {
                            coordJsonBuilder.AppendFormat("\"{0}\":[{1},{2}]", dest, Convert.ToDecimal(destCoordArr[0]), Convert.ToDecimal(destCoordArr[1]));
                        }
                        else
                        {
                            coordJsonBuilder.AppendLine();
                            coordJsonBuilder.AppendFormat(",\"{0}\":[{1},{2}]", dest, Convert.ToDecimal(destCoordArr[0]), Convert.ToDecimal(destCoordArr[1]));
                        }
                        deptList.Add(dest);
                    }
                }
            }
            coordJsonBuilder.Append("}");

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string project = Convert.ToString(dataTable.Rows[index][1]).Trim();
                string source = Convert.ToString(dataTable.Rows[index][4]).Trim();
                string dest = Convert.ToString(dataTable.Rows[index][5]).Trim();
                string valueString = Convert.ToString(dataTable.Rows[index][6]).Trim();
                string pm = Convert.ToString(dataTable.Rows[index][7]).Trim();
                string sourceGEO = Convert.ToString(dataTable.Rows[index][8]).Trim();
                string destGEO = Convert.ToString(dataTable.Rows[index][9]).Trim();

                if (s_importantProjects.Contains(project) == false)
                    continue;

                if (string.IsNullOrEmpty(destGEO) || string.IsNullOrEmpty(sourceGEO))
                    continue;

                decimal value = 0;
                if(decimal.TryParse(valueString, out value))
                {
                    string[] sourceCoordArr = sourceGEO.Split(new char[] { ',' });
                    string[] destCoordArr = destGEO.Split(new char[] { ',' });
                    if (sourceCoordArr.Length == 2 && destCoordArr.Length == 2)
                    {
                        if (projectJsonBuilder.Length == 1)
                        {
                            // [{name:'广州'},{name:'福州',value:95}]
                            projectJsonBuilder.AppendFormat("[{{\"name\":\"{0}\"}},{{\"name\":\"{1}\", \"value\":{2}}}]", source, dest, value);
                        }
                        else
                        {
                            projectJsonBuilder.AppendLine();
                            projectJsonBuilder.AppendFormat(",[{{\"name\":\"{0}\"}},{{\"name\":\"{1}\", \"value\":{2}}}]", source, dest, value);
                        }
                    }
                }
            }
            projectJsonBuilder.Append("]");
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"coordMap\":{0},\"links\":{1}}}", coordJsonBuilder, projectJsonBuilder);
            System.IO.File.WriteAllText(string.Format("{0}\\data-s5-project.json", AppDomain.CurrentDomain.BaseDirectory),
                builder.ToString());
            Console.WriteLine(string.Format("转换完毕,edges:{0}", dataTable.Rows.Count));
        }
    }
}
